package com.chengwang.bookstory.service.Imple;

import com.chengwang.bookstory.mapper.BookMapper;
import com.chengwang.bookstory.mapper.OrderMapper;
import com.chengwang.bookstory.mapper.ShopMapper;
import com.chengwang.bookstory.pojo.AllOrder;
import com.chengwang.bookstory.pojo.Book;
import com.chengwang.bookstory.pojo.MyShop;
import com.chengwang.bookstory.service.OrderService;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImple implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private BookMapper bookMapper;

    @Override
    public void buyShop(List<String> booksId,String userAccount) {
        //获取id下的得myshop信息
        List<MyShop> myShopList=shopMapper.queryMyShopById(booksId,userAccount);
        //将对应的id和num对应便于插入到订单中
        Map<String,Integer>IdNumMap=new HashMap<>();
        for (int i = 0; i <myShopList.size() ; i++) {
            IdNumMap.put(myShopList.get(i).getMyShop_bookId(),myShopList.get(i).getMyShopNumber());
        }
        System.out.println(IdNumMap);
        //更新shopList表
        shopMapper.update(booksId,userAccount);
        //更新库存
        List<Book> bookList = bookMapper.queryById(IdNumMap);
        for (int i = 0; i < bookList.size(); i++) {
           String id=bookList.get(i).getBookId();
           int num=bookList.get(i).getBookNumber()-IdNumMap.get(id);
            System.out.println(num);
           bookList.get(i).setBookNumber(num);
        }
        System.out.println(bookList);
        Map<String,Integer> bookIdNum=new HashMap<>();
        for (int i=0;i<bookList.size();i++){
            bookIdNum.put(bookList.get(i).getBookId(),bookList.get(i).getBookNumber());
        }
        for (int i = 0; i <bookList.size() ; i++) {
            bookMapper.updateByid(bookList.get(i).getBookNumber(),bookList.get(i).getBookId());
        }
        //添加到订单单表
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderId = df.format(new Date());
        orderMapper.addOderOne(IdNumMap,orderId);
        //订单总表
        Long allMoney= Long.valueOf(0);

        for (int i = 0; i <myShopList.size() ; i++) {
            allMoney+=(myShopList.get(i).getMyShopNumber())*(myShopList.get(i).getMyShop_price());
        }
        orderMapper.addOrderAll(userAccount,allMoney,orderId);
    }

    @Override
    public List<AllOrder> queryAllOrder(String userAccount) {
        return orderMapper.queryAllOrder(userAccount);
    }

    @Override
    public void deteAll(String userAccount, String orderId) {
        orderMapper.deteAll(userAccount,orderId);
    }

    @Override
    public void UpdateBad(String userAccount, String orderId) {
        orderMapper.UpdateBad(userAccount,orderId);
    }

    @Override
    public void UpdateOK(String userAccount, String orderId) {
        orderMapper.UpdateOK(userAccount,orderId);
    }

    @Override
    public List<AllOrder> mQueryAllOrder() {
        return orderMapper.mQueryAllOrder();
    }

    @Override
    public List<AllOrder> queryOrderByMoney() {

        return orderMapper.queryOrderByMoney();
    }

    @Override
    public List<AllOrder> queryAllOrderBySend() {
        orderMapper.sendOrder();
        return orderMapper.queryAllOrderBySend();
    }

    @Override
    public List<AllOrder> queryOrderByNO() {
        return orderMapper.queryOrderByNO();
    }

}
